/**
 * 在线提问
 */
var localStorageManagers = require('script/localStorageManagers.js');
var mobileFrame = require('scripts/Core/MobileFrame.js');
//mui初始化
mui.init({
	swipeBack: true
});
mui('.mui-scroll-wrapper').scroll({
					indicators: false //是否显示滚动条
				});
//plus初始化
mui.plusReady(function() {
	plusReady();
});
//plusReady
function plusReady() {
	//加载所有问题
	loadAllQuestion();
	//点击全部
	mui('.ques_top').on('tap', '.item_div.allques', loadAllQuestion);
	//我问的
	mui('.ques_top').on('tap', '.item_div.myques', loadMyQuestion);
	//我答的
	mui('.ques_top').on('tap', '.item_div.myanswerques', loadMyAnsQuestion);
	//教师转发
	mui('.ques_top').on('tap', '.item_div.zhangfaques', loadZhuanQuestion);
	//打开新增问题页面
	mui('.mui-content').on('tap','.addquesbtn',openAddQuesion);
	//打开问题详情
	mui('.mui-content').on('tap','.quesitem',openQuestionDetail);
	
}
//加载所有问题
function loadAllQuestion() {
	ajaxLoadQuestion(0);
}
//加载我提出的问题
function loadMyQuestion() {
	ajaxLoadQuestion(1);
}
//加载回答的问题
function loadMyAnsQuestion() {
	ajaxLoadQuestion(2);
}
//加载转发
function loadZhuanQuestion() {
	ajaxLoadQuestion(3);
}
//ajax加载所有问题
function ajaxLoadQuestion(type) {
	if(mobileFrame.IsNetWorkCanUse()){
		document.getElementById('currtype').value=type;
	    ajaxLoad();
	    
	   }
	    
}
//ajax加载
function ajaxLoad() {
	var type=document.getElementById('currtype').value;
	var userid = localStorageManagers.getUseridSession();
	var data = {
		userid: userid,
		type: type
	};
	var url = serverAjax + "mobile/studycenter/getOnlineQuestionList.do";
	var w=plus.nativeUI.showWaiting();
	mui.ajax(url, {
		data: data,
		type: 'post',
		dataType: 'json',
		timeout: 10000,
		success: function(data) {
			buildQuestionDiv(data);
			//mui('.elseinfo').imageLazyload({
			//	placeholder: '../../images/user_guest.png'
			//});
			w.close();
		},
		error: function(e1, e2, e3) {
			ajaxErrorTip();
			w.close();
			console.log(e1);
		}
	});
}
//构建问题列表div
function buildQuestionDiv(questions) {
	var quesListEle = document.getElementById('queslist');
	quesListEle.innerHTML = "";
	for (var i = 0; i < questions.length; i++) {
		var question = questions[i];
		var content=question.content;
		if(content.length>20)content=content.substring(0,20)+'...';
		var imgsrc="../../images/user_guest.png";
		if(question.userpath!=null&&question.userpath.length>0)imgsrc=question.userpath;
		var quesstr = '<div class="quesitem" questionid="'+question.qstId+'">' + '<div class="elseinfo"><span class="userimg"><img src="'+imgsrc+'"></span>' + '<p class="p1"><span class="c1">' + question.creator + '</span><span class="c3">回复</span><span class="c2" id="ansnum'+question.qstId+'">' + question.answerNum + '</span></p>' + '<p class="p2"><span class="c4">' + question.createTime + '</span></p>' + '</div>' + '<div class="content-value">' + content + '</div>' + '<div class="at_user">' + question.atUsers + '</div>' + '</div>';
		quesListEle.innerHTML = quesListEle.innerHTML + quesstr;
	}
}
//打开新增问题页面
function openAddQuesion(e){
    e.stopPropagation();
	if(!mobileFrame.IsNetWorkCanUse())return;
	mui.openWindow({
		url:'addquestion.html',
		id:'selfcenter/question/addquestion'
	});
}
//打开问题详情页
function openQuestionDetail(e){
	 e.stopPropagation();
	if(!mobileFrame.IsNetWorkCanUse())return;
	var quesid=this.getAttribute('questionid');
	mui.openWindow({
		url:'questiondetail.html',
		id:'selfcenter/question/questiondetail',
		extras:{quesid:quesid}
	});
}
//回复数量加一
function addAnswerNum(qstid){
	try{
	var quesEle=document.getElementById('ansnum'+qstid);
	quesEle.innerText=parseInt(quesEle.innerText)+1;
	}catch(e){
		
	}
}
